"""
权限枚举定义
统一维护系统权限的枚举、描述和分类
"""
from enum import Enum
from typing import Dict, List, Optional


class Permission(str, Enum):
    """权限枚举"""
    # 用户管理权限
    USER_CREATE = "user:create"
    USER_READ = "user:read"
    USER_UPDATE = "user:update"
    USER_DELETE = "user:delete"
    
    # 项目管理权限
    PROJECT_CREATE = "project:create"
    PROJECT_READ = "project:read"
    PROJECT_READ_ALL = "project:read_all"
    PROJECT_UPDATE = "project:update"
    PROJECT_UPDATE_ALL = "project:update_all"
    PROJECT_DELETE = "project:delete"
    PROJECT_MANAGE_MEMBERS = "project:manage_members"
    PROJECT_ARCHIVE = "project:archive"
    PROJECT_UNARCHIVE = "project:unarchive"
    PROJECT_STATS = "project:stats"
    PROJECT_ANALYTICS = "project:analytics"
    PROJECT_ADD_MEMBER = "project:add_member"
    PROJECT_REMOVE_MEMBER = "project:remove_member"
    PROJECT_UPDATE_MEMBER_ROLE = "project:update_member_role"
    PROJECT_CHANGE_STATUS = "project:change_status"
    PROJECT_CLOSE = "project:close"
    PROJECT_REOPEN = "project:reopen"
    PROJECT_CONFIGURE = "project:configure"
    PROJECT_SETTINGS = "project:settings"
    PROJECT_EXPORT = "project:export"
    PROJECT_IMPORT = "project:import"
    PROJECT_TEMPLATE_CREATE = "project:template_create"
    PROJECT_TEMPLATE_USE = "project:template_use"
    
    # 任务管理权限
    TASK_CREATE = "task:create"
    TASK_READ = "task:read"
    TASK_READ_ALL = "task:read_all"
    TASK_UPDATE = "task:update"
    TASK_DELETE = "task:delete"
    TASK_LOG_TIME = "task:log_time"
    TASK_ASSIGN = "task:assign"
    
    # 缺陷管理权限
    DEFECT_CREATE = "defect:create"
    DEFECT_READ = "defect:read"
    DEFECT_READ_ALL = "defect:read_all"
    DEFECT_UPDATE = "defect:update"
    DEFECT_DELETE = "defect:delete"
    DEFECT_ASSIGN = "defect:assign"
    
    # 测试用例管理权限
    TEST_CASE_CREATE = "test_case:create"
    TEST_CASE_READ = "test_case:read"
    TEST_CASE_READ_ALL = "test_case:read_all"
    TEST_CASE_UPDATE = "test_case:update"
    TEST_CASE_DELETE = "test_case:delete"
    TEST_CASE_ASSIGN = "test_case:assign"
    TEST_CASE_EXECUTE = "test_case:execute"
    
    # 需求管理权限
    REQUIREMENT_CREATE = "requirement:create"
    REQUIREMENT_READ = "requirement:read"
    REQUIREMENT_READ_ALL = "requirement:read_all"
    REQUIREMENT_UPDATE = "requirement:update"
    REQUIREMENT_DELETE = "requirement:delete"
    
    # 知识库权限
    DOC_CREATE = "doc:create"
    DOC_READ = "doc:read"
    DOC_UPDATE = "doc:update"
    DOC_DELETE = "doc:delete"
    
    # 系统管理权限
    SYSTEM_SETTINGS = "system:settings"
    SYSTEM_LOGS = "system:logs"
    SYSTEM_MANAGE = "system:manage"
    
    # 管理员权限
    ADMIN = "*"
    
    # 版本管理权限
    VERSION_CREATE = "version:create"
    VERSION_READ = "version:read"
    VERSION_READ_ALL = "version:read_all"
    VERSION_UPDATE = "version:update"
    VERSION_DELETE = "version:delete"
    VERSION_RELEASE = "version:release"
    VERSION_ROLLBACK = "version:rollback"
    
    # 版本审批权限
    VERSION_TRANSITION_REQUEST = "version:transition_request"
    VERSION_TRANSITION_APPROVE = "version:transition_approve"
    VERSION_TRANSITION_READ = "version:transition_read"
    VERSION_TRANSITION_CANCEL = "version:transition_cancel"
    VERSION_TRANSITION_REVOKE = "version:transition_revoke"
    
    # 评论管理权限
    COMMENT_CREATE = "comment:create"
    COMMENT_READ = "comment:read"
    COMMENT_UPDATE = "comment:update"
    COMMENT_DELETE = "comment:delete"
    
    # 角色管理权限
    ROLE_CREATE = "role:create"
    ROLE_READ = "role:read"
    ROLE_UPDATE = "role:update"
    ROLE_DELETE = "role:delete"
    ROLE_MANAGE = "role:manage"
    
    @classmethod
    def get_all_values(cls) -> List[str]:
        """获取所有权限值列表"""
        return [perm.value for perm in cls]
    
    @classmethod
    def is_valid(cls, permission: str) -> bool:
        """检查权限字符串是否有效"""
        try:
            cls(permission)
            return True
        except ValueError:
            return False


# 权限描述映射
PERMISSION_DESCRIPTIONS: Dict[str, str] = {
    # 用户管理权限
    Permission.USER_CREATE.value: "创建用户",
    Permission.USER_READ.value: "查看用户",
    Permission.USER_UPDATE.value: "更新用户",
    Permission.USER_DELETE.value: "删除用户",
    
    # 项目管理权限
    Permission.PROJECT_CREATE.value: "创建项目",
    Permission.PROJECT_READ.value: "查看项目",
    Permission.PROJECT_READ_ALL.value: "查看所有项目",
    Permission.PROJECT_UPDATE.value: "更新项目",
    Permission.PROJECT_UPDATE_ALL.value: "更新所有项目",
    Permission.PROJECT_DELETE.value: "删除项目",
    Permission.PROJECT_MANAGE_MEMBERS.value: "管理项目成员",
    Permission.PROJECT_ARCHIVE.value: "归档项目",
    Permission.PROJECT_UNARCHIVE.value: "取消归档项目",
    Permission.PROJECT_STATS.value: "查看项目统计",
    Permission.PROJECT_ANALYTICS.value: "查看项目分析",
    Permission.PROJECT_ADD_MEMBER.value: "添加项目成员",
    Permission.PROJECT_REMOVE_MEMBER.value: "移除项目成员",
    Permission.PROJECT_UPDATE_MEMBER_ROLE.value: "更新项目成员角色",
    Permission.PROJECT_CHANGE_STATUS.value: "更改项目状态",
    Permission.PROJECT_CLOSE.value: "关闭项目",
    Permission.PROJECT_REOPEN.value: "重新打开项目",
    Permission.PROJECT_CONFIGURE.value: "配置项目",
    Permission.PROJECT_SETTINGS.value: "项目设置",
    Permission.PROJECT_EXPORT.value: "导出项目",
    Permission.PROJECT_IMPORT.value: "导入项目",
    Permission.PROJECT_TEMPLATE_CREATE.value: "创建项目模板",
    Permission.PROJECT_TEMPLATE_USE.value: "使用项目模板",
    
    # 任务管理权限
    Permission.TASK_CREATE.value: "创建任务",
    Permission.TASK_READ.value: "查看任务",
    Permission.TASK_READ_ALL.value: "查看所有任务",
    Permission.TASK_UPDATE.value: "更新任务",
    Permission.TASK_DELETE.value: "删除任务",
    Permission.TASK_LOG_TIME.value: "记录工时",
    Permission.TASK_ASSIGN.value: "分配任务",
    
    # 缺陷管理权限
    Permission.DEFECT_CREATE.value: "创建缺陷",
    Permission.DEFECT_READ.value: "查看缺陷",
    Permission.DEFECT_READ_ALL.value: "查看所有缺陷",
    Permission.DEFECT_UPDATE.value: "更新缺陷",
    Permission.DEFECT_DELETE.value: "删除缺陷",
    Permission.DEFECT_ASSIGN.value: "分配缺陷",
    
    # 测试用例管理权限
    Permission.TEST_CASE_CREATE.value: "创建测试用例",
    Permission.TEST_CASE_READ.value: "查看测试用例",
    Permission.TEST_CASE_READ_ALL.value: "查看所有测试用例",
    Permission.TEST_CASE_UPDATE.value: "更新测试用例",
    Permission.TEST_CASE_DELETE.value: "删除测试用例",
    Permission.TEST_CASE_ASSIGN.value: "分配测试用例",
    Permission.TEST_CASE_EXECUTE.value: "执行测试用例",
    
    # 需求管理权限
    Permission.REQUIREMENT_CREATE.value: "创建需求",
    Permission.REQUIREMENT_READ.value: "查看需求",
    Permission.REQUIREMENT_READ_ALL.value: "查看所有需求",
    Permission.REQUIREMENT_UPDATE.value: "更新需求",
    Permission.REQUIREMENT_DELETE.value: "删除需求",
    
    # 知识库权限
    Permission.DOC_CREATE.value: "创建文档",
    Permission.DOC_READ.value: "查看文档",
    Permission.DOC_UPDATE.value: "更新文档",
    Permission.DOC_DELETE.value: "删除文档",
    
    # 系统管理权限
    Permission.SYSTEM_SETTINGS.value: "系统设置",
    Permission.SYSTEM_LOGS.value: "查看系统日志",
    Permission.SYSTEM_MANAGE.value: "系统管理",
    
    # 管理员权限
    Permission.ADMIN.value: "所有权限",
    
    # 版本管理权限
    Permission.VERSION_CREATE.value: "创建版本",
    Permission.VERSION_READ.value: "查看版本",
    Permission.VERSION_READ_ALL.value: "查看所有版本",
    Permission.VERSION_UPDATE.value: "更新版本",
    Permission.VERSION_DELETE.value: "删除版本",
    Permission.VERSION_RELEASE.value: "发布版本",
    Permission.VERSION_ROLLBACK.value: "回滚版本",
    
    # 版本审批权限
    Permission.VERSION_TRANSITION_REQUEST.value: "提交版本状态流转申请",
    Permission.VERSION_TRANSITION_APPROVE.value: "审批版本状态流转申请",
    Permission.VERSION_TRANSITION_READ.value: "查看版本审批记录",
    Permission.VERSION_TRANSITION_CANCEL.value: "取消版本流转申请",
    Permission.VERSION_TRANSITION_REVOKE.value: "撤销版本审批结果",
    
    # 评论管理权限
    Permission.COMMENT_CREATE.value: "创建评论",
    Permission.COMMENT_READ.value: "查看评论",
    Permission.COMMENT_UPDATE.value: "更新评论",
    Permission.COMMENT_DELETE.value: "删除评论",
    
    # 角色管理权限
    Permission.ROLE_CREATE.value: "创建角色",
    Permission.ROLE_READ.value: "查看角色",
    Permission.ROLE_UPDATE.value: "更新角色",
    Permission.ROLE_DELETE.value: "删除角色",
    Permission.ROLE_MANAGE.value: "管理角色",
}


# 权限模块映射
PERMISSION_MODULES: Dict[str, str] = {
    "user": "用户管理",
    "project": "项目管理",
    "task": "任务管理",
    "defect": "缺陷管理",
    "requirement": "需求管理",
    "doc": "知识库",
    "version": "版本管理",
    "system": "系统管理",
    "comment": "评论管理",
    "role": "角色管理",
    "test_case": "测试用例管理",
}


def get_permission_description(permission: str) -> str:
    """
    获取权限描述
    
    Args:
        permission: 权限字符串
        
    Returns:
        str: 权限描述，如果不存在返回权限字符串本身
    """
    return PERMISSION_DESCRIPTIONS.get(permission, permission)


def get_permission_module(permission: str) -> Optional[str]:
    """
    获取权限所属模块
    
    Args:
        permission: 权限字符串
        
    Returns:
        Optional[str]: 模块代码，如果无法确定返回None
    """
    if ":" in permission:
        module = permission.split(":")[0]
        if module in PERMISSION_MODULES:
            return module
    return None


def get_module_name(module_code: str) -> str:
    """
    获取模块名称
    
    Args:
        module_code: 模块代码
        
    Returns:
        str: 模块名称，如果不存在返回模块代码本身
    """
    return PERMISSION_MODULES.get(module_code, module_code)


def get_all_permissions() -> Dict[str, str]:
    """获取所有权限及描述"""
    return PERMISSION_DESCRIPTIONS.copy()


def get_all_modules() -> Dict[str, str]:
    """获取所有权限模块"""
    return PERMISSION_MODULES.copy()


def get_permissions_by_module(module_code: str) -> List[str]:
    """
    获取指定模块的所有权限
    
    Args:
        module_code: 模块代码
        
    Returns:
        List[str]: 权限列表
    """
    prefix = f"{module_code}:"
    return [
        perm for perm in PERMISSION_DESCRIPTIONS.keys()
        if perm.startswith(prefix) or perm == "*"
    ]

